package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "菜品套餐相关接口")
@Slf4j
@RestController
@RequestMapping("/admin/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @ApiOperation("根据id获取菜品套餐信息")
    @GetMapping("/{id}")
    public Result<SetmealVO> getSetMeal(@PathVariable Long id){
        SetmealVO setmealVO=setmealService.getByID(id);
        return Result.success(setmealVO);
    }

    @ApiOperation("新增菜品套餐信息")
    @PostMapping
    @CacheEvict(cacheNames = "setmealCache" ,key = "#setmealDTO.categoryId")
    public Result addSetMeal(@RequestBody SetmealDTO setmealDTO){
        setmealService.save(setmealDTO);
        return Result.success();
    }

    @ApiOperation("批量删除套餐信息")
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public  Result deleteSetMealBatch(@RequestParam List<Long> ids){
        setmealService.deleteBatch(ids);
        return Result.success();
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<PageResult> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO){
        PageResult pageResult =setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    @ApiOperation("修改套餐")
    @PutMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)

    public Result changeSetMeal(@RequestBody SetmealDTO setmealDTO){
        setmealService.modifySetmealwithDish(setmealDTO);
        return Result.success();
    }

    @ApiOperation("套餐起售和停售")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result stopOrOpen(@PathVariable Integer status,Long id){
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        setmealService.stopOrOpen(setmeal);
        return Result.success();
    }
}
